package com.lsh.queue;

import java.util.Scanner;

/**
 * @author ：LiuShihao
 * @date ：Created in 2020/7/29 2:47 下午
 * @desc ：设计环形数组实现队列
 */
public class CircleArrayDemo {
    public static void main(String[] args) {
        CircleArray queue = new CircleArray(3);
        char key = ' ';// 用于接收用户输入信息
        Scanner scanner = new Scanner(System.in);
        Boolean loop = true;
        while (loop){
            System.out.println("s(show()):显示队列");
            System.out.println("a(add()):添加数据到队列");
            System.out.println("g(get()):获得队列中数据");
            System.out.println("e(exit()):退出程序");
            System.out.println("h(head()):查看队列头");
            key = scanner.next().charAt(0);
            switch (key){
                case 's':
                    queue.showQueue();
                    break;
                case 'a':
                    System.out.println("请输入一个数");
                    int i = scanner.nextInt();
                    queue.add(i);
                    break;
                case 'g':
                    try {
                        int i2 = queue.get();
                        System.out.printf("取出的元素是%d\n",i2);
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'h':
                    try{
                        int head = queue.head();
                        System.out.printf("队列头是%d\n",head);
                    }catch (RuntimeException e){
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'e':
                    scanner.close();
                    loop = false;
                    break;
                default:
                    break;
            }
        }
        System.out.println("程序退出");
    }
}
class CircleArray{
    private int maxSize;//最大容量
    private int start;//队列头  初始值为0 默认指向队列的第一个元素
    private int end;//队列尾    初始值为0 指向队列的最后一个元素的后一个位置
    private int[] arr;//该数组用于存放数据
    public CircleArray(int arrMaxSize){
        maxSize = arrMaxSize;
        arr = new int[arrMaxSize];
    }
    public boolean isFull(){
        return (end+1)%maxSize == start;
    }
    public boolean isEmpty(){
        return start == end;
    }
    public void add(int n){
        if (isFull()){
            System.out.println("队列已满！");
            return;
        }
        arr[end] = n ;
        //将end后移，这里必须考虑取模
        end=(end+1)%maxSize;
    }
    public int get(){
        if (isEmpty()){
            throw  new RuntimeException("队列为空！");
        }
        //先把start对应的值赋值给临时变量
        //再将start后移，考虑取模
        //返回value
        int value = arr[start];
        start = (start+1)%maxSize;
        return value;
    }
    //展示队列的元素
    public void showQueue(){
        if (isEmpty()){
            System.out.println("队列为空！");
            return;
        }
        for (int i = start; i < start+size(); i++) {
            System.out.printf("arr[%d]=%d\n",i % maxSize,arr[i % maxSize]);
        }
    }
    //求出当前队列的有效元素个数
    public int size(){
        return (end+maxSize-start)% maxSize;
    }
    public int head(){
        if (isEmpty()){
            throw new RuntimeException("队列为空！");
        }
        return arr[start];
    }
        }